package com.nd.mobile.study.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.constants.SharedPreferenceConstants;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;
import com.nd.mobile.study.domain.ClassInfo;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class ClassListActivity extends BaseActivity {

	private TextView 		mListTitle;
	private ListView		mLV;
	private ClassAdapter	mAdapter;
	private ClassInfo[]		mClassInfos;
	private Intent			mIntent2Go;
	private int	mHeaderTitle;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.class_info);
	}

	@Override
	protected void initViews() {
		super.initHeader();
		getIntentData();
		mHeaderTitleTv.setText(mHeaderTitle);
		mHeaderBtnHome.setVisibility(View.VISIBLE);
		
		mListTitle = (TextView)findViewById(R.id.tv_list_title);
		mListTitle.setText("选择班级");
		
		mLV = (ListView) findViewById(R.id.common_lv);
	}

	private void getIntentData() {
		mHeaderTitle = getIntent().getIntExtra(BundleConstants.HEADER_TITLE, R.string.tv_app_myclass);
	}

	@Override
	protected void setFieldValues() {
		mIntent2Go = getIntent().setClass(this, (Class<?>) getIntent().getSerializableExtra(BundleConstants.NEXT_DEST));
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getClassInfo();
	}

	private void getClassInfo() {
		mLoading.p();
		new GetClassInfoTask(this).execute();
	}

	private class GetClassInfoTask extends SimpleAsyncTask<Void, Void, ClassInfo[]> {

		public GetClassInfoTask(Context context) {
			super(context);
		}

		@Override
		protected ClassInfo[] onLoad(Void... params) throws Exception {
			return App.getInstance().getUser().getClassInfo();
		}

		@Override
		protected void onUI(ClassInfo[] result) {
			mClassInfos = result;
			mAdapter = new ClassAdapter(mContext, mClassInfos);
			mLV.setAdapter(mAdapter);
			mLoading.v();
			mLV.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					ClassAdapter adapter = (ClassAdapter) parent.getAdapter();
					mIntent2Go.putExtra(BundleConstants.CLASS_INFO, adapter.getData().get(position));
					startActivity(mIntent2Go);
				}
			});
		}

	}

	private class ClassAdapter extends SimpleListAdapter<ClassInfo> {

		public ClassAdapter(Context context, ClassInfo[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvName;
			TextView	tvDescr;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.class_list_item, null);

				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.class_list_tv_name);
				holder.tvDescr = (TextView) convertView.findViewById(R.id.class_list_tv_descr);
				
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ClassInfo currData = mData.get(position);
			holder.tvName.setText(currData.getName());
			holder.tvDescr.setText(currData.getDescr());
			
			return convertView;
		}
	}
}
